package com.jic.market.impl.rest.promotion.promotionInfoHandler;

import com.beust.jcommander.internal.Lists;
import com.jic.common.base.vo.PageResult;
import com.jic.common.utils.DateUtils;
import com.jic.market.common.enums.DeleteFlagEnum;
import com.jic.market.common.enums.MutexFlagEnum;
import com.jic.market.entity.PatchGrouponMpEntity;
import com.jic.market.entity.PatchGrouponThemeEntity;
import com.jic.market.entity.PromotionEntity;
import com.jic.market.enums.FrontPromotionTypeEnum;
import com.jic.market.enums.PromotionProductTypeEnum;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.enums.PromotionTypeEnum;
import com.jic.market.mapper.PatchGrouponMpEntityMapper;
import com.jic.market.mapper.PatchGrouponThemeEntityMapper;
import com.jic.market.orm.Pagination;
import com.jic.market.request.rest.promotion.PromotionInfoRestRequest;
import com.jic.market.response.rest.promotion.PromotionDetailRestResponse;
import com.jic.market.response.rest.promotion.PromotionGoodsSpuRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * description: PatchGrouponHandler
 * date: 2020/4/8 16:38
 * author: wangys
 * version: 1.0
 *  拼团
 */
@Service
@Slf4j
public class PatchGrouponHandler extends BaseInfoHandlerTemplate {

    @Autowired
    PatchGrouponMpEntityMapper patchGrouponMpEntityMapper;

    @Autowired
    PatchGrouponThemeEntityMapper patchGrouponThemeEntityMapper;

    final static Integer GROUPON_PROM_TYPE = PromotionTypeEnum.GROUPON.getCode();

    final static Integer GROUPON_ING_STSTUS = PromotionStatusEnum.ING.getCode();

    @Override
    public String handlerCode() {
        return "patchGroupon_handler";
    }

    @Override
    public List<Integer> frontPromTypes() {
//        return Lists.newArrayList(
//            FrontPromotionTypeEnum.GROUPON
//        ).stream().map(FrontPromotionTypeEnum::getCode).collect(Collectors.toList());
        return Lists.newArrayList();
    }

    @Override
    public boolean enable() {
        return true;
    }

    @Override
    public List<PromotionDetailRestResponse> apply(PromotionInfoRestRequest request) {
        return null;
    }

    private PromotionDetailRestResponse genPromotionDetailRestResponse(PatchGrouponThemeEntity a) {
        PromotionDetailRestResponse restResponse = new PromotionDetailRestResponse()
                .setEndTime(a.getEndTime().getTime())
                .setStartTime(a.getStartTime().getTime())
                .setPromotionCode(a.getGroupCode())
                .setPromotionType(PROM_TYPE_MAP.get(a.getPromType()))
                .setFrontPromotionType(FRONT_PROM_TYPE_MAP.get(a.getFrontPromType()))
                .setStartTimeStr(DateUtils.getFormatDateTime(a.getStartTime()))
                .setEndTimeStr(DateUtils.getFormatDateTime(a.getEndTime()));
        restResponse.setDescription(genDescription());
        return restResponse;
    }

    /**
     * 根据活动类型和规则生成描述
     * 拼团
     * @return
     */
    private String genDescription(){
        return null;
    }

    /**
     * 根据活动code获取活动详情
     * @param request
     * @return
     */
    @Override
    public PromotionDetailRestResponse promotionDetail(PromotionInfoRestRequest request) {
        PatchGrouponThemeEntity themeEntity = new PatchGrouponThemeEntity();
        themeEntity.setGroupCode(request.getPromotionCode());
        return genPromotionDetailRestResponse(patchGrouponThemeEntityMapper.query(themeEntity));
    }

    /**
     * 根据商品code获取进行中的活动列表
     * @param request
     * @return
     */
    @Override
    public List<PromotionEntity> promotionBaseDetail(PromotionInfoRestRequest request) {
        List<PromotionEntity> promotionEntities = new ArrayList<>();
//        PatchGrouponMpEntity patchGrouponMpEntity = new PatchGrouponMpEntity();
//        patchGrouponMpEntity.setThirdSpuSkuId(request.getThirdProductCode());
//        patchGrouponMpEntity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
//        patchGrouponMpEntity.setMutexFlag(MutexFlagEnum.UN_MUTEX.getCode());
//        patchGrouponMpEntity.setTypeOfProduct(request.getProductType());
//        List<PatchGrouponMpEntity> patchGrouponMpEntities = patchGrouponMpEntityMapper.findAll(patchGrouponMpEntity);
//        //如果是spu，是否互斥需要根据sku判断
//        if(request.getProductType().equals(PromotionProductTypeEnum.TYPE_SPU.getType())){
//            patchGrouponMpEntities = patchGrouponMpEntities.stream()
//                    .filter(x->{
//                        PatchGrouponMpEntity skuParam = new PatchGrouponMpEntity();
//                        skuParam.setPromotionId(x.getPromotionId());
//                        skuParam.setMpParentId(x.getMpId());
//                        skuParam.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
//                        skuParam.setMutexFlag(MutexFlagEnum.UN_MUTEX.getCode());
//                        return patchGrouponMpEntityMapper.countOfPage(skuParam) > 0;
//                    })
//                    .collect(Collectors.toList());
//        }
//
//        if(CollectionUtils.isEmpty(patchGrouponMpEntities)){
//            return promotionEntities;
//        }
//
//        // 查询出进行中的活动 然后根据promotionCodes过滤
//        PatchGrouponThemeEntity themeEntity = new PatchGrouponThemeEntity();
//        themeEntity.setPromType(GROUPON_PROM_TYPE);
//        themeEntity.setStatus(GROUPON_ING_STSTUS);
//        themeEntity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
//        themeEntity.setIds(patchGrouponMpEntities.stream().map(PatchGrouponMpEntity::getPromotionId).collect(Collectors.toSet()));
//        List<PatchGrouponThemeEntity> patchGrouponThemeEntitys = patchGrouponThemeEntityMapper.findOfPageForRest(themeEntity, Pagination.build(1, Integer.MAX_VALUE));
//        if(CollectionUtils.isEmpty(patchGrouponThemeEntitys)){
//            return promotionEntities;
//        }
//        patchGrouponThemeEntitys.stream()
//                .forEach(a->{
//                    PromotionEntity promotionEntity = new PromotionEntity();
//                    promotionEntity.setPromotionCode(a.getGroupCode());
//                    promotionEntity.setPromType(a.getPromType());
//                    promotionEntity.setFrontPromType(a.getFrontPromType());
//                    promotionEntity.setCreateTime(a.getCreateTime());
//                    promotionEntity.setId(a.getId());
//                    promotionEntities.add(promotionEntity);
//                });
        return promotionEntities;
    }

    @Override
    public PromotionEntity getPromotionDetailByPromotionCode(String promotionCode) {
        return null;
    }

    @Override
    public PageResult<PromotionGoodsSpuRestResponse> getGoodsByPromotionCode(PromotionInfoRestRequest request, Long promotionId) {
      return null;
    }
}
